package org.feng.fxfront;

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.stage.Stage;
import org.feng.fxfront.pages.MainPage;
import org.feng.fxfront.pages.animal.AnimalManagePage;
import org.feng.fxfront.sys.PageNodeRenderFactory;
import org.feng.fxfront.util.ArrayUtil;
import org.feng.fxfront.util.StyleUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 启动类
 *
 * @author Administrator
 */
public class StartApplication extends Application {

    /**
     * 屏幕宽度
     */
    public static final Double SCREEN_WIDTH;

    /**
     * 屏幕高度
     */
    public static final Double SCREEN_HEIGHT;

    /**
     * 左侧菜单宽度
     */
    public static final Double LEFT_MENU_WIDTH;

    @Override
    public void start(Stage stage) {
        // 注册页面渲染器
        registerPageNodeRender();

        root = new HBox();
        // 渲染左侧菜单栏
        leftMenu = renderLeftMenu(root);
        root.getChildren().add(leftMenu);

        // 主面板
        currentPageNode = routePage(root, MainPage.PAGE_KEY);
        HBox.setHgrow(currentPageNode, Priority.ALWAYS);
        root.getChildren().add(currentPageNode);
        StyleUtil.setPaneBackground(root, Color.WHITE);

        Scene scene = new Scene(root, SCREEN_WIDTH * 0.8, SCREEN_HEIGHT * 0.8);

        // 窗口的标题
        stage.setTitle("管理系统");
        stage.setScene(scene);
        // 禁止拖拽窗口大小
        stage.setResizable(false);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }


    /**
     * 渲染左侧菜单栏
     */
    private VBox renderLeftMenu(HBox root) {
        VBox vbox = new VBox();
        vbox.setMinHeight(root.getPrefHeight());
        vbox.setMinWidth(LEFT_MENU_WIDTH);
        StyleUtil.setPaneBackground(vbox, LEFT_MENU_BACKGROUND_COLOR);
        // 增加菜单中的项目
        vbox.getChildren().addAll(getLeftMenuItemList());
        return vbox;
    }

    /**
     * 生成左侧菜单按钮
     */
    private List<Button> getLeftMenuItemList() {
        double buttonHeight = 30;
        List<Button> buttonList = new ArrayList<>(3);
        Map<String, String> pageNameMap = PageNodeRenderFactory.getPageNameMap();
        String[] itemNames = pageNameMap.values().toArray(new String[0]);

        for (String name : itemNames) {
            Button button = new Button(name);
            button.setMinWidth(LEFT_MENU_WIDTH);
            button.setMinHeight(buttonHeight);
            StyleUtil.setButtonBackground(button, LEFT_MENU_BUTTON_BACKGROUND_COLOR, Color.WHITE);
            // 增加鼠标移动到菜单上到hover效果
            button.setOnMouseMoved(event -> {
                StyleUtil.setButtonBackground(button, LEFT_MENU_BUTTON_HOVER_BACKGROUND_COLOR, Color.WHITE);
                StyleUtil.setFont(button, Color.web("#E6A23C"), -1);
            });
            button.setOnMouseExited(event -> {
                if (currentMenuIndex == null || !button.getText().equals(itemNames[currentMenuIndex])) {
                    StyleUtil.setButtonBackground(button, LEFT_MENU_BUTTON_BACKGROUND_COLOR, Color.WHITE);
                } else {
                    StyleUtil.setButtonBackground(button, LEFT_MENU_BUTTON_BACKGROUND_COLOR, Color.WHITE);
                }
            });
            button.setOnMouseClicked(event -> {
                currentMenuIndex = ArrayUtil.getIndexForArray(itemNames, button.getText());
                currentPageNode = routePage(root, PageNodeRenderFactory.getPageKeyByName(name));
                root.getChildren().remove(1);    //清除右侧页面路由组件节点
                HBox.setHgrow(currentPageNode, Priority.ALWAYS);
                root.getChildren().add(currentPageNode);
                StyleUtil.setFont(button, Color.WHITE, -1);
                // 选中状态逻辑
                if (tempIndex != null) {
                    Button node = (Button) leftMenu.getChildren().get(tempIndex);
                    // 清空选中状态样式
                    StyleUtil.setFont(node, Color.WHITE, -1);
                    StyleUtil.setButtonBackground(node, LEFT_MENU_BUTTON_BACKGROUND_COLOR, Color.WHITE);
                }
                // 设置选中样式
                StyleUtil.setFont(button, LEFT_MENU_BUTTON_CLICK_BACKGROUND_COLOR, -1);
                tempIndex = currentMenuIndex;
            });
            buttonList.add(button);
        }
        return buttonList;
    }

    /**
     * 右侧页面路由
     */
    private Node routePage(Pane root, String pageKey) {
        return PageNodeRenderFactory.newInstance(pageKey).render(root);
    }

    private void registerPageNodeRender() {
        PageNodeRenderFactory.registerNodeRender(MainPage.class);
        PageNodeRenderFactory.registerNodeRender(AnimalManagePage.class);

        PageNodeRenderFactory.fillPageNameMap();
    }


    /**
     * root容器，使用HBox类型
     */
    private HBox root;

    /**
     * 左侧菜单栏
     */
    private VBox leftMenu;

    /**
     * 当前页的节点
     */
    private Node currentPageNode;

    /**
     * 左侧菜单的当前索引
     */
    private Integer currentMenuIndex;
    /**
     * 左侧菜单的临时索引，选中时的菜单项索引
     */
    private Integer tempIndex;

    /**
     * 左侧菜单面板背景颜色
     */
    private static final Color LEFT_MENU_BACKGROUND_COLOR = Color.web("#FFF8DC");
    /**
     * 左侧菜单按钮背景颜色
     */
    private static final Color LEFT_MENU_BUTTON_BACKGROUND_COLOR = Color.web("#DEB887");
    /**
     * 左侧菜单按钮hover时（鼠标移动到按钮上）的颜色
     */
    private static final Color LEFT_MENU_BUTTON_HOVER_BACKGROUND_COLOR = Color.web("D2691E");
    /**
     * 左侧菜单按钮点击时的字体颜色
     */
    private static final Color LEFT_MENU_BUTTON_CLICK_BACKGROUND_COLOR = Color.web("#6495ed");

    static {
        SCREEN_WIDTH = Screen.getPrimary().getBounds().getWidth();
        SCREEN_HEIGHT = Screen.getPrimary().getBounds().getHeight();
        LEFT_MENU_WIDTH = 240.0;
    }
}
